package com.android.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.R;
import com.android.common.Action;
import com.android.common.JsInterface;

public class MainWebActivity extends Activity implements
		OnMenuItemClickListener {
	private WebView mWebView;
	private ProgressBar mProgressBar;
	Button btn_back, btn_submit;
	TextView mTitle;
	Action action;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_web_view);

		action = new Action(MainWebActivity.this);

		initViews();
		// flag = getIntent().getIntExtra("url", 0);

		loadUrl();
	}

	private void initViews() {
		mTitle = (TextView) findViewById(R.id.titlebar_name);
		mTitle.setVisibility(View.VISIBLE);
		mTitle.setText("首页");
		btn_back = (Button) findViewById(R.id.titlebar_back);
		btn_back.setVisibility(View.VISIBLE);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		btn_submit.setVisibility(View.GONE);
		mWebView = (WebView) findViewById(R.id.webView1);
		mProgressBar = (ProgressBar) findViewById(R.id.titlebar_progress);
		mProgressBar.setVisibility(View.VISIBLE);
	}

	public void clickBtn(View v) {
		switch (v.getId()) {
		case R.id.titlebar_back:
			if (mWebView.canGoBack())
				mWebView.goBack();
			else {
				action.exit(MainWebActivity.this);
			}

			break;
		case R.id.btn_submit:
			action.showShortToast("您点击了提交按钮");
			break;

		default:
			break;
		}
	}

	private void loadUrl() {
		// 设置对JS的支持
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		// webView.loadUrl("http://10.10.12.250/t/index.php");

		mWebView.loadUrl("http://diaoyan.jxzyk.cn/wap/login.asp");

		mWebView.requestFocus();// 如果不设置，则在点击网页文本输入框时，不能弹出软键盘及不响应其他的一些事件。 取消滚动条
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

		mWebView.setVerticalScrollBarEnabled(true);

		mWebView.getSettings()
				.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

		mWebView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.d("monkey1", url);
				view.loadUrl(url);

				// CookieSyncManager.createInstance(this);
				CookieManager cookieManager = CookieManager.getInstance();
				String loginCookie = cookieManager.getCookie(url);
				// cookieManager.setCookie("name", "sfsf");
				// Toast.makeText(MainActivity.this, loginCookie, 0).show();
				// Toast.makeText(MainActivity.this, url, 0).show();

				return true;
			}

			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				Log.d("monkey2", url);
				action.startProgressDialog("正在加载");
				mProgressBar.setVisibility(View.VISIBLE);
				btn_submit.setVisibility(View.GONE);

			}

			public void onPageFinished(WebView view, String url) {
				Log.d("monkey3", url);
				action.stopProgressDialog();
				mProgressBar.setVisibility(View.GONE);
				btn_submit.setVisibility(View.VISIBLE);
			}
		});

		mWebView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int newProgress) {
				mProgressBar.setProgress(newProgress);
			}

			public void onReceivedIcon(WebView view, Bitmap icon) {
			}

			public void onReceivedTitle(WebView view, String title) {
				mTitle.setText(title);
			}
		});

		mWebView.addJavascriptInterface(new JsInterface(mWebView,
				MainWebActivity.this), "android");
	}

	public boolean onMenuItemClick(MenuItem item) {
		switch (item.getItemId()) {
		// 向后（back）
		case 0:
			mWebView.goBack();
			break;
		// 向前（Forward）
		case 1:
			mWebView.goForward();
			break;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem miBack = menu.add(0, 0, 0, "向后（back）");
		MenuItem miForward = menu.add(0, 1, 1, "向前（Forward）");
		miBack.setOnMenuItemClickListener(this);
		miForward.setOnMenuItemClickListener(this);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	// 设置回退
	// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		} else if ((keyCode == KeyEvent.KEYCODE_BACK) && !mWebView.canGoBack()) {
			action.exit(MainWebActivity.this);
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		action.stopProgressDialog();
		if (mWebView != null) {
			mWebView = null;
		}
		super.onDestroy();
	}

}
